package com.aurora.source.center.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatButton;
import android.view.View;
import android.widget.EditText;

import com.aurora.source.R;
import com.aurora.source.base.BaseWhiteToolbarActivity;
import com.aurora.source.center.presenter.AddBatchPresenter;
import com.aurora.source.center.view.IAddBatchView;
import com.aurora.source.util.SPUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class AddBatchActivity extends BaseWhiteToolbarActivity<AddBatchPresenter> implements IAddBatchView {

    @BindView(R.id.et_batchNo)
    EditText etBatchNo;
    @BindView(R.id.et_batchTitle)
    EditText etBatchTitle;
    @BindView(R.id.ap_confirm)
    AppCompatButton apConfirm;
    boolean isEdit;//是否是修改
    String batchName;
    @Override
    public AddBatchPresenter attach() {
        return new AddBatchPresenter(this);
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_add_batch;
    }

    @Override
    public void initData(@Nullable Bundle bundle) {
        isEdit = bundle.getBoolean("isEdit",false);
        batchName = bundle.getString("batchName");
    }

    @Override
    public void initView(Bundle savedInstanceState, View contentView) {
        if(isEdit){
            simpleToolbar.setMainTitle("批次管理-修改批次");
            etBatchTitle.setText(batchName);
            apConfirm.setText("确认修改");
        }else {
            simpleToolbar.setMainTitle("批次管理-添加批次");
        }

        simpleToolbar.setLeftVisibility(View.VISIBLE);
    }

    @Override
    public void doBusiness() {

    }


    @OnClick({R.id.ap_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ap_confirm:
                if(isEdit){
                    Intent intent = new Intent();
                    intent.putExtra("batchName",etBatchTitle.getText().toString());
                    setResult(RESULT_OK,intent);
                    hideSoftInput();
                    finish();
                }else {
                    Map<String, String> params = new HashMap<>();
                    params.put("userId", (String) SPUtils.get(activity, SPUtils.USER_ID, ""));
                    //   params.put("batchNo", etBatchNo.getText().toString());
                    params.put("batchTitle", etBatchTitle.getText().toString());
                    params.put("number ","1");
                    basePresenter.addBAtch(activity, params);
                }
                break;
        }
    }

    @Override
    public void onAddBatchFail(String msg) {
        showToast(msg);
    }

    @Override
    public void onAddBatchSuccess(String msg) {
        showToast(msg);
        hideSoftInput();
        finish();
    }

}
